package com.tianqu.tms.www.transfer.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferWorkCount;
import com.tianqu.tms.www.transfer.viewhelper.TransferWorkCountViewHelper;

public class TransferWorkCountExportAction extends TmsAction {

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		
		//根据userID查询导出
		String countMonth = request.getParameter("countMonth");
		
		TransferWorkCountViewHelper view = (TransferWorkCountViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		
        // 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("工作量统计");  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        style.setBorderBottom((short) 1);   
        style.setBorderTop((short) 1);
        style.setBorderLeft((short) 1);
        style.setBorderRight((short) 1); 
        
        sheet.setDefaultRowHeightInPoints(10);
        sheet.setDefaultColumnWidth(6);
        
        HSSFFont  font  =  wb.createFont(); 
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        font.setFontHeightInPoints((short)8);
        style.setFont(font);
          
        HSSFCellStyle cellStyle = wb.createCellStyle();  
		HSSFFont titleFont = wb.createFont();
		titleFont.setFontName("宋体");
		titleFont.setFontHeight((short) 200);
		titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        
        cellStyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        cellStyle.setBorderBottom((short) 1);   
        cellStyle.setBorderTop((short) 1);
        cellStyle.setBorderLeft((short) 1);
        cellStyle.setBorderRight((short) 1); 
        cellStyle.setFont(titleFont);

        HSSFCell cell1 = row.createCell((short) 0);  
        cell1.setCellValue("姓名");  
        cell1.setCellStyle(cellStyle); 
        
        HSSFCell cell2 = row.createCell((short) 1);  
        cell2.setCellValue("岗位");  
        cell2.setCellStyle(cellStyle); 
        HSSFCell cell3 = row.createCell((short) 2);  
        cell3.setCellValue("1");  
        cell3.setCellStyle(cellStyle); 
        HSSFCell cell4 = row.createCell((short) 3);  
        cell4.setCellValue("2");  
        cell4.setCellStyle(cellStyle); 
        HSSFCell cell5 = row.createCell((short) 4);  
        cell5.setCellValue("3");  
        cell5.setCellStyle(cellStyle); 
        HSSFCell cell6 = row.createCell((short) 5);  
        cell6.setCellValue("4");  
        cell6.setCellStyle(cellStyle); 
        HSSFCell cell7 = row.createCell((short) 6);  
        cell7.setCellValue("5");  
        cell7.setCellStyle(cellStyle); 
        HSSFCell cell8 = row.createCell((short) 7);  
        cell8.setCellValue("6");  
        cell8.setCellStyle(cellStyle); 
        HSSFCell cell9 = row.createCell((short) 8);  
        cell9.setCellValue("7");  
        cell9.setCellStyle(cellStyle); 
        HSSFCell cell10 = row.createCell((short) 9);  
        cell10.setCellValue("8");  
        cell10.setCellStyle(cellStyle); 
        HSSFCell cell11 = row.createCell((short) 10);  
        cell11.setCellValue("9");  
        cell11.setCellStyle(cellStyle); 
        HSSFCell cell12 = row.createCell((short) 11);  
        cell12.setCellValue("10");  
        cell12.setCellStyle(cellStyle); 
        HSSFCell cell13 = row.createCell((short) 12);  
        cell13.setCellValue("11");  
        cell13.setCellStyle(cellStyle); 
        HSSFCell cell14 = row.createCell((short) 13);  
        cell14.setCellValue("12");  
        cell14.setCellStyle(cellStyle); 
        HSSFCell cell15 = row.createCell((short) 14);  
        cell15.setCellValue("13");  
        cell15.setCellStyle(cellStyle); 
        HSSFCell cell16 = row.createCell((short) 15);  
        cell16.setCellValue("14");  
        cell16.setCellStyle(cellStyle); 
        HSSFCell cell17 = row.createCell((short) 16);  
        cell17.setCellValue("15");  
        cell17.setCellStyle(cellStyle); 
        HSSFCell cell18 = row.createCell((short) 17);  
        cell18.setCellValue("16");  
        cell18.setCellStyle(cellStyle); 
        HSSFCell cell19 = row.createCell((short) 18);  
        cell19.setCellValue("17");  
        cell19.setCellStyle(cellStyle); 
        HSSFCell cell20 = row.createCell((short) 19);  
        cell20.setCellValue("18");  
        cell20.setCellStyle(cellStyle); 
        HSSFCell cell21 = row.createCell((short) 20);  
        cell21.setCellValue("19");  
        cell21.setCellStyle(cellStyle); 
        HSSFCell cell22 = row.createCell((short) 21);  
        cell22.setCellValue("20");  
        cell22.setCellStyle(cellStyle); 
        HSSFCell cell23 = row.createCell((short) 22);  
        cell23.setCellValue("21");  
        cell23.setCellStyle(cellStyle); 
        HSSFCell cell24 = row.createCell((short) 23);  
        cell24.setCellValue("22");  
        cell24.setCellStyle(cellStyle); 
        HSSFCell cell25 = row.createCell((short) 24);  
        cell25.setCellValue("23");  
        cell25.setCellStyle(cellStyle); 
        HSSFCell cell26 = row.createCell((short) 25);  
        cell26.setCellValue("24");  
        cell26.setCellStyle(cellStyle); 
        HSSFCell cell27 = row.createCell((short) 26);  
        cell27.setCellValue("25");  
        cell27.setCellStyle(cellStyle); 
        HSSFCell cell28 = row.createCell((short) 27);  
        cell28.setCellValue("26");  
        cell28.setCellStyle(cellStyle); 
        HSSFCell cell29 = row.createCell((short) 28);  
        cell29.setCellValue("27");  
        cell29.setCellStyle(cellStyle); 
        HSSFCell cell30 = row.createCell((short) 29);  
        cell30.setCellValue("28");  
        cell30.setCellStyle(cellStyle); 
        HSSFCell cell31 = row.createCell((short) 30);  
        cell31.setCellValue("29");  
        cell31.setCellStyle(cellStyle); 
        HSSFCell cell32 = row.createCell((short) 31);  
        cell32.setCellValue("30");  
        cell32.setCellStyle(cellStyle); 
        HSSFCell cell33 = row.createCell((short) 32);  
        cell33.setCellValue("31");  
        cell33.setCellStyle(cellStyle); 
        HSSFCell cell34 = row.createCell((short) 33);  
        cell34.setCellValue("总计");  
        cell34.setCellStyle(cellStyle); 
        
        TransferWorkCount w = new TransferWorkCount();
        List<TransferWorkCount> list = w.getWorkCountListByMonth(countMonth,view.getUserid());
        
        for(int i=0;i<list.size();i++)
        {
        	TransferWorkCount entity = (TransferWorkCount)list.get(i);
        	row = sheet.createRow(i+1);
        	
        	HSSFCell c0 = row.createCell((short) 0);
        	c0.setCellStyle(style);
        	c0.setCellValue(entity.getUserName()); 
	        HSSFCell c2 = row.createCell((short) 1);
	        c2.setCellStyle(style);
	        c2.setCellValue(entity.getWorkType());
	        HSSFCell c3 = row.createCell((short) 2);
	        c3.setCellStyle(style);
	        c3.setCellValue(entity.getCount1());
	        HSSFCell c4 = row.createCell((short) 3);
	        c4.setCellStyle(style);
	        c4.setCellValue(entity.getCount2());
        	HSSFCell c5 = row.createCell((short) 4);
        	c5.setCellStyle(style);
        	c5.setCellValue(entity.getCount3());
        	HSSFCell c6 = row.createCell((short) 5);
        	c6.setCellStyle(style);
        	c6.setCellValue(entity.getCount4());
        	
        	HSSFCell c7 = row.createCell((short) 6);
        	c7.setCellStyle(style);
        	c7.setCellValue(entity.getCount5());
        	
        	HSSFCell c8 = row.createCell((short) 7);
        	c8.setCellStyle(style);
        	c8.setCellValue(entity.getCount6());
        	
        	HSSFCell c9 = row.createCell((short) 8);
        	c9.setCellStyle(style);
        	c9.setCellValue(entity.getCount7());
        	
        	HSSFCell c10 = row.createCell((short) 9);
        	c10.setCellStyle(style);
        	c10.setCellValue(entity.getCount8());
        	
        	HSSFCell c11 = row.createCell((short) 10);
        	c11.setCellStyle(style);
        	c11.setCellValue(entity.getCount9());
        	
        	HSSFCell c12 = row.createCell((short) 11);
        	c12.setCellStyle(style);
        	c12.setCellValue(entity.getCount10());
        	
        	HSSFCell c13 = row.createCell((short) 12);
        	c13.setCellStyle(style);
        	c13.setCellValue(entity.getCount11());
        	
        	HSSFCell c14 = row.createCell((short) 13);
        	c14.setCellStyle(style);
        	c14.setCellValue(entity.getCount12());
        	
        	HSSFCell c15 = row.createCell((short) 14);
        	c15.setCellStyle(style);
        	c15.setCellValue(entity.getCount13());
        	
        	HSSFCell c16 = row.createCell((short) 15);
        	c16.setCellStyle(style);
        	c16.setCellValue(entity.getCount14());
        	
        	HSSFCell c17 = row.createCell((short) 16);
        	c17.setCellStyle(style);
        	c17.setCellValue(entity.getCount15());
        	
        	HSSFCell c18 = row.createCell((short) 17);
        	c18.setCellStyle(style);
        	c18.setCellValue(entity.getCount16());
        	
        	HSSFCell c19 = row.createCell((short) 18);
        	c19.setCellStyle(style);
        	c19.setCellValue(entity.getCount17());
        	
        	HSSFCell c20 = row.createCell((short) 19);
        	c20.setCellStyle(style);
        	c20.setCellValue(entity.getCount18());
        	
        	HSSFCell c21 = row.createCell((short) 20);
        	c21.setCellStyle(style);
        	c21.setCellValue(entity.getCount19());
        	
        	HSSFCell c22 = row.createCell((short) 21);
        	c22.setCellStyle(style);
        	c22.setCellValue(entity.getCount20());
        	
        	HSSFCell c23 = row.createCell((short) 22);
        	c23.setCellStyle(style);
        	c23.setCellValue(entity.getCount21());
        	
        	HSSFCell c24 = row.createCell((short) 23);
        	c24.setCellStyle(style);
        	c24.setCellValue(entity.getCount22());
        	
        	HSSFCell c25 = row.createCell((short) 24);
        	c25.setCellStyle(style);
        	c25.setCellValue(entity.getCount23());
        	
        	HSSFCell c26 = row.createCell((short) 25);
        	c26.setCellStyle(style);
        	c26.setCellValue(entity.getCount24());
        	
        	HSSFCell c27 = row.createCell((short) 26);
        	c27.setCellStyle(style);
        	c27.setCellValue(entity.getCount25());
        	
        	HSSFCell c28 = row.createCell((short) 27);
        	c28.setCellStyle(style);
        	c28.setCellValue(entity.getCount26());
        	
        	HSSFCell c29 = row.createCell((short) 28);
        	c29.setCellStyle(style);
        	c29.setCellValue(entity.getCount27());
        	
        	HSSFCell c30 = row.createCell((short) 29);
        	c30.setCellStyle(style);
        	c30.setCellValue(entity.getCount28());
        	
        	HSSFCell c31 = row.createCell((short) 30);
        	c31.setCellStyle(style);
        	c31.setCellValue(entity.getCount29());
        	
        	HSSFCell c32 = row.createCell((short) 31);
        	c32.setCellStyle(style);
        	c32.setCellValue(entity.getCount30());
        	
        	HSSFCell c33 = row.createCell((short) 32);
        	c33.setCellStyle(style);
        	c33.setCellValue(entity.getCount31());
        	
        	HSSFCell c34 = row.createCell((short) 33);
        	c34.setCellStyle(style);
        	c34.setCellValue(entity.getSum());
        }
        try  
        {  
        	File file =new File(toUtf8String("工作量统计"+view.getCountMonth()+".xls"));
        	OutputStream  fout = new FileOutputStream(file);  
            wb.write(fout); 
            getResponse(file, response);
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        } 
//        uc.setAttribute("tsxx", "1");
        request.setAttribute(TmsConstants.VIEWHELP, view);
		return null;
	}
}